#!/bin/sh
#
# @file tc-docker
# @version v1.0
# @description tinycore (tc) in docker
# @author innovarew <box> (c) Copyright 2022.
# @copyright All rights reserved. Read LICENSE.txt file for the license terms.
#
# Changelog:
#
# [dd/mm/yyyy]        [author]
# [brief description]
#
# 26/08/2022        innovarew
# v1.0 Initial version
#

PROGNAME=`basename "$0"`;
PROGAUTHOR="innovarew <box> (c) 2022."
PROGVERSION=v1.2;

[ -z "$TC_URL" ] && TC_URL=http://tinycorelinux.net/14.x/x86_64/
ISO_URL=$TC_URL/release/TinyCorePure64-current.iso
TCZ_URL=$TC_URL/tcz

TCE_DIR=$PWD/data/tce/optional
if grep -qi tinycore /etc/os-release
then
    TCE_DIR=/tmp/tce/optional
fi
[ ! -d $TCE_DIR ] && mkdir -p $TCE_DIR

tce_rootfs_init()
{
    TC_VER=$(echo $TC_URL | awk -F/ '{print $4"-"$5}')
    TC_TAR="rootfs-$TC_VER.tar.xz"
    TC_PATCH="rootfs-$TC_VER.patch"

    echo "# $ISO_URL"
    (
        rm -r data/tce
        mkdir -p $TCE_DIR

        rm -r data/root
        rm -r data/tinycore

        mkdir -p data
        cd data

        # unpack rootfs core.gz
        tce_rootfs_unpack "$@"

        # rootfs contains .ko.gz not used by docker
        find $PWD/root/ -name '*.ko.gz' -exec rm {} \;

        # get unsquashfs into container to unpack tcz
        tce_install liblzma.tcz $PWD/root/
        tce_install squashfs-tools.tcz $PWD/root/

        patch -p2 < $TC_PATCH
    )

    (cd data/root; tar cJf ../$TC_TAR .)
}

tce_rootfs_unpack()
{
    ISO_FILE=$(basename $ISO_URL)

    wget -qc "$ISO_URL"
    # wget -qc "$ISO_URL.md5.txt" && md5sum -c `basename "$ISO_URL.md5.txt"`

    if [ ! -d "tinycore" ]
    then
        mkdir -p tinycore
        7z x -otinycore $ISO_FILE >/dev/null
        mkdir -p root
        (cd root; zcat ../tinycore/boot/corepure64.gz | cpio --extract)
    fi
}

tce_cddir()
{
    if [ -n "$PWD" -a "$PWD" != "$TCE_DIR" ]
    then
        cd $TCE_DIR
    fi
}

# usage: tce_install squashfs-tools.tcz /
tce_install()
{
    [ $# -lt 2 ] && usage

    local app=$1
    local root=$2

    if [ -e $root/usr/local/tce.installed/${app%.tcz} ]
    then
        usage "warn: $app already installed in $root"
    fi

    (
        tce_cddir

        if wget -qc $TCZ_URL/$app.dep
        then
            for dep in `cat $app.dep`
            do
                tce_install $dep $root
            done
        fi

        wget -qc $TCZ_URL/$app
        wget -qc $TCZ_URL/$app.md5.txt
        ! md5sum -c $app.md5.txt && return

        unsquashfs -n -d "$root" -f $app >/dev/null

        # update system
        mkdir -p $root/usr/local/tce.installed/
        touch $root/usr/local/tce.installed/${app%.tcz}

        # update libs
        if [ -w /etc/ld.so.cache ]
        then
            ldconfig
        fi
    )
}

tce_info()
{
    [ $# -lt 1 ] && usage
    local app=$1
    (
        tce_cddir
        [ ! -e $app.info ] && wget -qc $TCZ_URL/$app.info
        cat $app.info
    )
}

tce_list()
{
    [ $# -lt 1 ] && usage
    local app=$1
    (
        tce_cddir
        [ ! -e $app.list ] && wget -qc $TCZ_URL/$app.list
        cat $app.list
    )
}

tce_dep()
{
    [ $# -lt 1 ] && usage
    local app=$1
    (
        tce_cddir
        [ ! -e $app.list ] && wget -qc $TCZ_URL/$app.dep
        cat $app.dep
    )
}

tce_search()
{
    (
        tce_cddir
        wget -qc $TCZ_URL
        cat `basename $TCZ_URL`
    )
}

usage()
{
    if [ $# -eq 0 ]
    then
        echo "usage: $PROGNAME [tce_rootfs_init|tce_install] # @version $PROGVERSION @copyright $PROGAUTHOR";
        exit;
    else
        echo "$@"
        return
    fi
}

main()
{
    [ $# -lt 1 ] && usage

    "$@"
}

main "$@"
